<!--
 * @Descripttion: 领券记录
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 14:04:40
-->
<template>
    <div class="lb-sys-sproconfig">
        <top-nav :title="navTitle" :isBack="true"></top-nav>
        <div class="page-main">
          <div class="page-search-form">
            <el-form
            :inline="true"
            :model="searchForm"
            ref="searchForm"
            >
              <el-form-item label="状态" prop="status">
                <el-select v-model="searchForm.status" placeholder="请选择">
                    <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="福利类型" prop="type">
                <el-select v-model="searchForm.type" placeholder="请选择">
                    <el-option
                    v-for="item in typeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="用户昵称" prop="nickName">
                <el-input v-model="searchForm.nickName" placeholder="请输入用户昵称"></el-input>
              </el-form-item>
              <el-form-item>
                <lb-button size="medium" type="primary" icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
                <lb-button size="medium" icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')">{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </div>

            <el-table
            v-loading="loading"
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
            <el-table-column
              prop="nickName"
              label="用户昵称">
            </el-table-column>
            <el-table-column
              prop="coupon_name"
              label="福利券">
            </el-table-column>
            <el-table-column
              prop="type"
              label="类型">
              <template slot-scope="scope">
                <span>{{scope.row.type === 1 ? '新客户福利':'推荐人福利'}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="领取时间">
              <template slot-scope="scope">
                <p>{{scope.row.create_time | handleTime(1)}}</p>
                <p>{{scope.row.create_time | handleTime(2)}}</p>
              </template>
            </el-table-column>
            <el-table-column
              prop="status_text"
              label="状态">
              <!-- <template slot-scope="scope">
                <span>{{scope.row.status === 1 ? '未使用' :scope.row.status === 2 ? '已使用':'已过期'}}</span>
              </template> -->
            </el-table-column>
            <el-table-column
              prop="hx_name"
              label="核销人">
            </el-table-column>
          </el-table>

        <lb-page
          :batch='false'
          :page='searchForm.page'
          :pageSize='searchForm.limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
        </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      statusOptions: [
        {label: '全部', value: 0},
        {label: '未使用', value: 1},
        {label: '已使用', value: 2},
        {label: '已过期', value: 3}
      ],
      typeOptions: [
        {label: '全部', value: 0},
        {label: '新客户福利', value: 1},
        {label: '推荐人福利', value: 2}
      ],
      loading: false,
      total: 0,
      searchForm: {
        type: 0,
        status: 0,
        nickName: '',
        id: 0,
        page: 1,
        limit: 10
      },
      tableData: []
    }
  },
  activated () {
    let { id } = this.$route.query
    if (id) {
      this.searchForm.id = id
      this.getTableDataList()
    }
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let {searchForm} = this
      let {code, data} = await this.$api.presentCouponList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
      this.navTitle = `“${data.active_name}”的领券记录`
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input,
.el-select{
  width: 200px;
}
</style>
